<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
        //取我发的帖
        $sql = 'select * from questions where uid = '.$_SESSION['id'];
        $question = M('questions')->query($sql);
        $this->assign('question',$question);

        //取我收藏的帖子
        $sql = "select t1.*,t2.title from collection t1 inner join questions t2 on t1.qid = t2.id where t1.uid = ".$_SESSION['id'];
        $collection = M()->query($sql);
        $this->assign('collection',$collection);
    	
        
        $this->assign('title','用户中心');
        $this->display();
    }

    public function set()
    {	
        $res = M('user')->where(array('id'=>$_SESSION['id']))->find();
        $this->assign('ziliao',$res);
    	// var_dump(I('post.'));
    	$this->assign('title','设置');
        $this->display();
    }

    public function checkset(){
    	$res = M('user')->where(array('id'=>$_SESSION['id']))->find();
    	//验证邮箱
    	if ($res['email']!=I('post.email')) {
    		if (D('user')->checkEmail(I('post.email'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"该邮箱已存在"
    			);
    		echo json_encode($arr);
    		exit;
    		}
    	}

    	if ($_SESSION['username']!=I('post.username')) {
    		if (D('user')->checkName(I('post.username'))) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"该昵称已存在"
    			);
    		echo json_encode($arr);
    		exit;
    		}
    	}


    	// var_dump($_SESSION['id']);
		if (M('user')->where('id='.$_SESSION['id'])->save(I('post.'))) {
			// var_dump(M('user')->where('id='.$_SESSION['id'])->save(I('post.')));
			$_SESSION['username']=I('post.username');
            $_SESSION['email']=I('post.email');
            $_SESSION['sex']=I('post.sex');
    		$arr = array(
                'error'=>0,
                'info'=>"修改成功"
                );
            echo json_encode($arr);
        }else{
            $arr = array(
                'error'=>1,
                'info'=>"没有修改"
                );
            echo json_encode($arr);
        }

    	// var_dump(array_values();
    	// var_dump(array_values(I('post.')));
		// if (M('user')->MUser($data)) {
  //           $_SESSION['username']=$data['username'];
  //           $_SESSION['email']=$data['email'];
  //           $_SESSION['headface']=$data['headface'];
  //   		$arr = array(
  //   			'error'=>0,
  //   			'info'=>"修改成功"
  //   			);
  //   		echo json_encode($arr);
  //   	}else{
  //   		$arr = array(
  //   			'error'=>1,
  //   			'info'=>"修改失败"
  //   			);
  //   		echo json_encode($arr);
  //   	}
    		
    }


    public function checkpass()
    {
    	// var_dump(I('post.'));
    	if (I('post.pass')!=I('post.repass')) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"密码不一致"
    			);
    		echo json_encode($arr);
    		exit();
    	} 
    	$pass['pass'] = md5(I('post.pass'));
    	$pass['id'] = $_SESSION['id'];
    	// var_dump($pass);
    	// exit();
    	if (M('user')->save($pass)) {
    		$arr = array(
    			'error'=>0,
    			'info'=>"修改成功"
    			);
    		echo json_encode($arr);
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>"修改失败"
    			);
    		echo json_encode($arr);
    	}
    }

    //修改头像
    public function headface (){
        // var_dump($_FILES);
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
            $upload->savePath  =     '/headface/'; // 设置附件上传（子）目录
            // 上传文件 
            $info   =   $upload->upload();
            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }else{// 上传成功
                $info = current($info);
                $info = '/Uploads'.$info['savepath'].$info['savename'];
                // var_dump($info);

                M('user')->where(array("id"=>$_SESSION['id']))->save(array('headface'=>$info));
                $_SESSION['headface']=$info;
                echo (0);
            }
    }
   
}


